﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using ChapterX.Common;
using ChapterX.Data;
using ChapterX.Data.BooksDSTableAdapters;
using ChapterX.Data.ChaptersDSTableAdapters;
using ChapterX.Data.CoursesDSTableAdapters;
using ChapterX.Data.DigitizationDSTableAdapters;

namespace ChapterX.BusinessFacade
{
    public class BookSystem
    {
        private BooksDS.BooksDataTable _bookSchema = null;
        public BooksDS.BooksDataTable BookSchema
        {
            get
            {
                if (null == _bookSchema)
                {
                    _bookSchema = new BooksDS.BooksDataTable();
                }
                return _bookSchema;
            }
        }

        #region Book

        public BooksDS.BooksRow GetBookByBookId(int bookId)
        {
            BooksTableAdapter adapter = new BooksTableAdapter();
            BooksDS.BooksDataTable table = adapter.GetBookByBookId(bookId);

            if (null != table && table.Rows.Count > 0)
            {
                return table[0];
            }
            else
            {
                return null;
            }
        }

        public bool SubmitBook(BooksDS.BooksRow book, String submitUser)
        {
            bool result = false;

            try
            {
                if (Verify(book))
                {
                    BooksTableAdapter adapter = new BooksTableAdapter();

                    if (book.BookId < 0)
                    {
                        // 教材信息不存在，插入教材信息
                        result = InsertBook(adapter, book, submitUser);
                    }
                    else
                    {
                        // 教材信息已存在，更新教材信息
                        result = UpdateBook(adapter, book, submitUser);
                    }
                }
            }
            catch (VerifyException ex)
            {
                throw ex;
            }
            catch (UnauthorizedException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new GenericException(ex.Message);
            }

            return result;
        }

        protected bool InsertBook(BooksTableAdapter adapter, BooksDS.BooksRow book, String submitUser)
        {
            bool result = false;

            try
            {
                int? bookId = null;
                adapter.InsertBook(ref bookId,
                    book.Title,
                    book.AuthorEditor,
                    book.IsISBNNull() ? null : book.ISBN,
                    book.IsPublisherNull() ? null : book.Publisher,
                    book.IsPubPlaceNull() ? null : book.PubPlace,
                    book.IsPubYearNull() ? (int?)null : book.PubYear,
                    book.IsEditionNull() ? null : book.Edition,
                    book.TeacherId, 
                    submitUser);
                
                if (bookId.HasValue)
                {
                    book.BookId = bookId.Value;
                    result = true;
                }
            }
            catch
            {
                result = false;
            }

            return result;
        }

        protected bool UpdateBook(BooksTableAdapter adapter, BooksDS.BooksRow book, String updateUser)
        {
            bool result = false;

            try
            {
                result = (adapter.UpdateBook(book.BookId,
                    book.Title,
                    book.AuthorEditor,
                    book.IsISBNNull() ? null : book.ISBN,
                    book.IsPublisherNull() ? null : book.Publisher,
                    book.IsPubPlaceNull() ? null : book.PubPlace,
                    book.IsPubYearNull() ? (int?)null : book.PubYear,
                    book.IsEditionNull() ? null : book.Edition,
                    book.TeacherId,
                    updateUser) == 1);
            }
            catch
            {
                result = false;
            }

            return result;
        }

        public bool DeleteBook(int bookId)
        {
            bool result = false;

            try
            {
                BooksTableAdapter adapter = new BooksTableAdapter();
                result = (adapter.Delete(bookId) == 1);
            }
            catch
            {
                result = false;
            }

            return result;
        }

        protected bool Verify(BooksDS.BooksRow book)
        {
            return true;
        }

        public bool HasCourses(int bookId)
        {
            bool result = false;

            CourseBookViewTableAdapter adpater = new CourseBookViewTableAdapter();
            DataTable table;
            if (CurrentUserInfo.IsLibrarian)
            {
                table = adpater.GetCourseListByBookId(bookId);
            }
            else
            {
                table = adpater.GetCourseListByBookIdAndTeacherId(bookId, CurrentUserInfo.Username);
            }

            if (table != null && table.Rows.Count > 0)
                result = true;

            return result;
        }

        #endregion
    }
}
